package com.auerdi;

import android.app.Application;
import android.content.ContextWrapper;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.egghttp.EggLog.L;
import com.example.egghttp.retrofit.interceptor.Token;
import com.pixplicity.easyprefs.library.Prefs;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.PermissionListener;
import com.yhao.floatwindow.Screen;
import com.yhao.floatwindow.ViewStateListener;

import me.yokeyword.fragmentation.Fragmentation;
import me.yokeyword.fragmentation.helper.ExceptionHandler;

/**
 * Created by tse on 16/11/23.
 */
public class App extends Application {
    private PermissionListener mPermissionListener = new PermissionListener() {
        @Override
        public void onSuccess() {
            L.d("hcia", "onSuccess");
        }

        @Override
        public void onFail() {
            L.d("hcia", "onFail");
        }
    };

    private ViewStateListener mViewStateListener = new ViewStateListener() {
        @Override
        public void onPositionUpdate(int x, int y) {
            L.d("hcia", "onPositionUpdate: x=" + x + " y=" + y);
        }

        @Override
        public void onShow() {
            L.d("hcia", "onShow");
        }

        @Override
        public void onHide() {
            L.d("hcia", "onHide");
        }

        @Override
        public void onDismiss() {
            L.d("hcia", "onDismiss");
        }

        @Override
        public void onMoveAnimStart() {
            L.d("hcia", "onMoveAnimStart");
        }

        @Override
        public void onMoveAnimEnd() {
            L.d("hcia", "onMoveAnimEnd");
        }

        @Override
        public void onBackToDesktop() {
            L.d("hcia", "onBackToDesktop");
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();


        new Prefs.Builder()
                .setContext(this)
                .setMode(ContextWrapper.MODE_PRIVATE)
                .setPrefsName(getPackageName())
                .setUseDefaultSharedPreference(true)
                .build();


        Fragmentation.builder()
                // 设置 栈视图 模式为 （默认）悬浮球模式   SHAKE: 摇一摇唤出  NONE：隐藏， 仅在Debug环境生效
                .stackViewMode(Fragmentation.NONE)
                .debug(true) // 实际场景建议.debug(BuildConfig.DEBUG)
                /**
                 * 可以获取到{@link me.yokeyword.fragmentation.exception.AfterSaveStateTransactionWarning}
                 * 在遇到After onSaveInstanceState时，不会抛出异常，会回调到下面的ExceptionHandler
                 */
                .handleException(new ExceptionHandler() {
                    @Override
                    public void onException(Exception e) {
                        // 以Bugtags为例子: 把捕获到的 Exception 传到 Bugtags 后台。
                        // Bugtags.sendException(e);
                    }
                })
                .install();


//        setFolatBt();
    }

    private void setFolatBt() {
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setImageResource(R.mipmap.logo);


        FloatWindow
                .with(getApplicationContext())
                .setView(imageView)
                .setWidth(Screen.width, 0.2f) //设置悬浮控件宽高
                .setHeight(Screen.width, 0.2f)
                .setX(Screen.width, 0.8f)
                .setY(Screen.height, 0.3f)
                .setMoveType(MoveType.slide, -100, -100)
                .setMoveStyle(500, new AccelerateInterpolator())
                .setFilter(true, MainActivity.class)
                .setViewStateListener(mViewStateListener)
                .setPermissionListener(mPermissionListener)
                .setDesktopShow(true)
                .build();


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Token.setToken("");
                Toast.makeText(App.this, "test logout !", Toast.LENGTH_SHORT).show();
            }
        });
    }

//    @GlideModule
//    public static final class MyAppGlideModule extends AppGlideModule {
//    }
}
